package com.loanapp.server.market.webapp.controller.bbs;


import com.loanapp.base.common.constants.AppConstant;
import com.loanapp.base.common.dto.ResponseDto;
import com.loanapp.base.common.exception.BizExceptionEnum;
import com.loanapp.base.common.exception.BusinessException;
import com.loanapp.base.common.utils.StringUtil;
import com.loanapp.base.controller.support.HttpKit;
import com.loanapp.base.controller.utils.PageUtil;
import com.loanapp.server.market.api.bbs.api.CommentApi;
import com.loanapp.server.market.api.bbs.api.PostApi;
import com.loanapp.server.market.api.bbs.entity.Comment;
import com.loanapp.server.user.api.user.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("comment")
@Api(value = "论坛评论控制器", description = "论坛评论API")
public class CommentController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private CommentApi commentApi;

    @Autowired
    private PostApi postApi;



    @ApiOperation(value = "添加论坛评论", notes="只需填必填项",
            produces = "application/json; charset=utf-8")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "postId", value = "论坛帖子id",
                    dataType = "int", paramType = "query", defaultValue="",required = true),
            @ApiImplicitParam(name = "content", value = "论坛评论内容",
                    dataType = "int", paramType = "query", defaultValue="",required = true),
            @ApiImplicitParam(name = "token", value = "token秘钥",
                    dataType = "String", paramType = "query", defaultValue="",required = true),
    })
    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ResponseBody
    public ResponseDto add(Comment comment, @RequestParam String token) throws Exception {
        ResponseDto result=ResponseDto.build(BizExceptionEnum.SUCCESSFUL_OPERATION);
          String sso_key= HttpKit.getRequest().getHeader("Authorization");
          if(StringUtil.isEmpty(sso_key)){
              throw  new BusinessException(BizExceptionEnum.NO_THIS_USER);
          }
          User user= (User) redisTemplate.opsForValue().get(sso_key);
          if(user==null){
              return ResponseDto.build(BizExceptionEnum.REQUEST_FAILED);
          }
          if(user!=null){
              comment.setPostPid(user.getId());
              comment.setPostPname(user.getPhone());
          }
          else {
              return ResponseDto.build(BizExceptionEnum.REQUEST_FAILED);
          }

         boolean canPost= postApi.CanPost(user.getId());

          if(canPost){
              Long rs= commentApi.insert(comment);
              if(rs==null) {
                  return ResponseDto.build(BizExceptionEnum.REQUEST_FAILED);
              }
          }
          else {
              return ResponseDto.build(AppConstant.Code.FAIL,"你已被禁言，请联系管理员");
          }
        return result;
    }

    @ApiOperation(value = "帖子评论", notes="",
            produces = "application/json; charset=utf-8")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页多少行（默认值5）",
                    dataType = "int", paramType = "query", defaultValue=""),
            @ApiImplicitParam(name = "pageNum", value = "当前页",
                    dataType = "int", paramType = "query", defaultValue=""),
            @ApiImplicitParam(name = "pid", value = "帖子ID",
                    dataType = "int", paramType = "query", defaultValue="",required = true),
    })
    @RequestMapping(value = "postComment",method = RequestMethod.POST)
    @ResponseBody
    public ResponseDto getCommentByPid(@RequestParam Long pid, HttpServletRequest request) throws Exception {
        ResponseDto result=ResponseDto.build(BizExceptionEnum.SUCCESSFUL_OPERATION);
            if(StringUtil.isEmpty(pid)){
                return ResponseDto.build(AppConstant.Code.FAIL,AppConstant.Msg.FAIL);
            }
            List<Map<String,Object>> list=commentApi.getCommentByPid(pid, PageUtil.init(request));
            result.setData(list);
        return result;
    }
}
